package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Painter;
import ve.com.wayoyo.wuml_case.UserPref;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

public class CompSubEntidad extends Componente {

	static final int textMargin = 15;

	private Paint _borderPaint = Painter.getInstancia().getDefaultBorderPaint();
	private RectF _border = new RectF();
	private String longestString;
	private ArrayList<CompAtributoER> _atributos = new ArrayList<CompAtributoER>();

	public CompSubEntidad(String Titulo) {
		if (UserPref.getInstancia().isUsingConvections() && Titulo.length() > 1) {
			this.Titulo = Titulo.toUpperCase();
		}
		Point punto = new Point();
		punto.x = 20;
		punto.y = 20;
		this.setAnchor(punto);
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(Titulo)
				+ textMargin;
		longestString = Titulo;

		_centerPoint = new Point();
		_topAnchors = new BorderAnchors(4, Anchor, new Point(
				(int) (Anchor.x + _width), Anchor.y));
		_bottomAnchors = new BorderAnchors(4, new Point(Anchor.x,
				(int) (Anchor.y + _height)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));
		_leftAnchors = new BorderAnchors(4, Anchor, new Point((int) (Anchor.x),
				(int) (Anchor.y + _height)));
		_rightAnchors = new BorderAnchors(4, new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));

		// Nuevo Metodo de Conexion
		_connectionPoints = new Point[16];
		_connectionPointsCost = new int[16];
		float cpDistX = _width / 5;
		float cpDistY = _height / 5;
		for (int i = 0; i < 4; i++) {
			_connectionPoints[i] = new Point(
					(int) (Anchor.x + ((i + 1) * cpDistX)), Anchor.y);
		}
		for (int i = 4; i < 8; i++) {
			_connectionPoints[i] = new Point((int) (Anchor.x + _width),
					(int) (Anchor.y + ((i - 3) * cpDistY)));
		}
		for (int i = 8; i < 12; i++) {
			_connectionPoints[i] = new Point(
					(int) ((Anchor.x + _width) - ((i - 7) * cpDistX)),
					(int) (Anchor.y + _height));
		}
		for (int i = 12; i < 16; i++) {
			_connectionPoints[i] = new Point(Anchor.x,
					(int) ((Anchor.y + _height) - ((i - 11) * cpDistY)));
		}

		setTitulo(Titulo);

	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;
		Paint _textPaint = Painter.getInstancia().getTextPaint(false,
				globalScale);

		_width = _textPaint.measureText(longestString) / globalScale
				+ textMargin;

		_border.set((posX) * globalScale, (posY) * globalScale, (posX + _width)
				* globalScale, (posY + _height) * globalScale);
		c.drawRoundRect(_border, 15 * globalScale, 15 * globalScale, Painter
				.getInstancia().getBorderPaint(_currentState));

		// Dibujamos la linea del titulo
		// c.drawLine(posX*globalScale,(posY+30)*globalScale,(posX+_width)*globalScale,(posY+30)*globalScale,_borderPaint);

		try {
			// Dibujamos el titulo
			float textSize = _textPaint.measureText(Titulo);
			float distToMid = (((_width) * globalScale) - textSize) / 2;
			c.drawText(Titulo, (posX * globalScale) + distToMid, (posY + 25)
					* globalScale, _textPaint);
		} catch (Exception e) {
			float textSize = _textPaint.measureText(" ");
			float distToMid = (((_width) * globalScale) - textSize) / 2;
			c.drawText(" ", (posX * globalScale) + distToMid, (posY + 25)
					* globalScale, _textPaint);
		}

		// Dibujamos los atributos
		int puntoEscritura = 0;
		for (CompAtributoER value : _atributos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX + 5) * globalScale,
					(posY + 30 + puntoEscritura) * globalScale, _textPaint);
		}

		// Dibujamos Los conectores Padre
		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}

	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		boolean Visible = true;
		if ((Anchor.x + _width) <= offSetX) {
			Visible = false;
		} else if (Anchor.x <= offSetX + UserPref.getInstancia().getWinWidth()) {
			Visible = false;
		} else if (Anchor.y > offSetY + UserPref.getInstancia().getWinHeight()) {
			Visible = false;
		} else if ((Anchor.y + _height) <= offSetY) {
			Visible = false;
		}
		return Visible;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
				// FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		_topAnchors.actualizar(Anchor, new Point((int) (Anchor.x + _width),
				Anchor.y));
		_bottomAnchors.actualizar(new Point(Anchor.x,
				(int) (Anchor.y + _height)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));
		_leftAnchors.actualizar(Anchor, new Point((int) (Anchor.x),
				(int) (Anchor.y + _height)));
		_rightAnchors.actualizar(new Point((int) (Anchor.x + _width),
				(int) (Anchor.y)), new Point((int) (Anchor.x + _width),
				(int) (Anchor.y + _height)));

		float cpDistX = _width / 5;
		float cpDistY = _height / 5;
		for (int i = 0; i < 4; i++) {
			_connectionPoints[i].set((int) (Anchor.x + ((i + 1) * cpDistX)),
					Anchor.y);
		}
		for (int i = 4; i < 8; i++) {
			_connectionPoints[i].set((int) (Anchor.x + _width),
					(int) (Anchor.y + ((i - 3) * cpDistY)));
		}
		for (int i = 8; i < 12; i++) {
			_connectionPoints[i].set(
					(int) ((Anchor.x + _width) - ((i - 7) * cpDistX)),
					(int) (Anchor.y + _height));
		}
		for (int i = 12; i < 16; i++) {
			_connectionPoints[i].set((int) (Anchor.x),
					(int) ((Anchor.y + _height) - ((i - 11) * cpDistY)));
		}
		if (VistaDiagrama.getVista() != null) {
			Diagrama diagramaActual = VistaDiagrama.getVista().getDiagrama();
			for (CompConector value : _conectorsBegining) {
				value.actualizarRuta(this);
			}
			for (CompConector value : _conectorsEnd) {
				value.actualizarRuta(diagramaActual.getComponenteById(value
						.get_fatherId()));
			}
		}

	}

	@Override
	public void actualizarPuntoCentral() {
		_centerPoint.set((Anchor.x + (int) (_width / 2)),
				(Anchor.y + (int) (_height / 2)));

	}

	@Override
	public String generarDOM() {
		String DOM = "\t\t<SubComponente Titulo=\"" + Titulo + "\" ID=\"" + ID
				+ "\" AnchorX=\"" + Anchor.x + "\" AnchorY=\"" + Anchor.y
				+ "\">\n";
		for (CompAtributoER value : _atributos) {
			DOM = DOM + "\t\t" + value.generarDOM(Titulo);
		}
		DOM = DOM + "\t\t</SubComponente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		_width = textPaint.measureText(Titulo) + textMargin;
		longestString = Titulo;
		_height = 80;
		if (_atributos.size() == 0) {
			_height += 15;
		} else {
			for (CompAtributoER atr : _atributos) {
				if (_width - 10 < textPaint.measureText(atr.getText())) {
					_width = textPaint.measureText(atr.getText()) + textMargin;
					longestString = atr.getText();
				}
				_height += 30;
			}
		}
		actualizarAchorsPoints();
		actualizarPuntoCentral();

	}

	@Override
	public void setTitulo(String titulo) {
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		if (UserPref.getInstancia().isUsingConvections()) {
			this.Titulo = titulo.toUpperCase();
			if (_width - textMargin < textPaint.measureText(Titulo)) {
				_width = textPaint.measureText(Titulo) + textMargin;
				longestString = Titulo;

			}
		} else {
			this.Titulo = titulo;
			if (_width - textMargin < textPaint.measureText(Titulo)) {
				_width = textPaint.measureText(Titulo) + textMargin;
				longestString = Titulo;
			}
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
		if (VistaDiagrama.getVista() != null) {
			CompEntidad padre = ((DiagramaER) VistaDiagrama.getVista()
					.getDiagrama()).get_selectedItemFather();
			if (padre != null) {
				padre.actualizarPuntoCentral();
				padre.actualizarAchorsPoints();
			}
		}

	}

	public void eliminarAtributo(int position) {
		this.get_atributos().remove(position);
		calculoLongitud();
	}

	public ArrayList<CompAtributoER> get_atributos() {
		return _atributos;
	}

	public void set_atributos(ArrayList<CompAtributoER> _atributos) {
		this._atributos = _atributos;
	}

	public void addAtributo(CompAtributoER comp) {
		_atributos.add(comp);
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		if (_width - textMargin < textPaint.measureText(comp.getText())) {
			_width = textPaint.measureText(comp.getText()) + textMargin;
			longestString = comp.getText();
		}
		_height += 30;
		actualizarPuntoCentral();
		actualizarAchorsPoints();

	}

	@Override
	public String getTipo() {
		return "Entidad";
	}

	public Boolean validAtribNew(CompAtributoER atr) {
		// Falta validar las del padre
		for (CompAtributoER atrib : this._atributos) {
			if (atrib.getNombreVariable().equals(atr.getNombreVariable())) {
				return false;
			}
		}
		return true;
	}

	public Boolean validAtrib(CompAtributoER atr) {
		// Falta validar las del padre
		Boolean flag = false;
		for (CompAtributoER atrib : this._atributos) {
			if (atrib.getNombreVariable().equals(atr.getNombreVariable())) {
				if (!flag)
					flag = true;
				else
					return false;
			}
		}
		return true;
	}

}
